package cn.brilliantfuture.commentry.inform.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Remove;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cn.brilliantfuture.commentry.entity.EmailInfo;
import cn.brilliantfuture.commentry.entity.SmsInfo;
import cn.brilliantfuture.commentry.inform.InformMessageLocal;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2013-6-28
 * 
 * Description:
 * 
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class InformMessageImpl implements InformMessageLocal{

	@PersistenceContext(unitName = "commentry-jpa")
	EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> queryInformMessage(Integer language, Integer use, Integer applyOrient) {
		Map<String, Object> map = new HashMap<String, Object>();

		List<SmsInfo> smsInfos = entityManager.createQuery("select smsInfo from SmsInfo smsInfo where deleted = 0 and smsInfo.languageInfo.languageInfoId = :languageInfoId and smsInfo.smsUse = :smsUse and smsInfo.applyOrient = :applyOrient").setParameter("languageInfoId", language).setParameter("smsUse", use).setParameter("applyOrient", applyOrient).getResultList();
			
		if (smsInfos.size() > 0){
			map.put(SMS_INFORM, smsInfos.get(0));
		} 

		List<EmailInfo> emailInfos  = entityManager.createQuery("select emailInfo from EmailInfo emailInfo where deleted = 0 and emailInfo.languageInfo.languageInfoId = :languageInfoId and emailInfo.emailUse = :emailUse and emailInfo.applyOrient = :applyOrient").setParameter("languageInfoId", language).setParameter("emailUse", use).setParameter("applyOrient", applyOrient).getResultList();
			
		if (emailInfos.size() > 0){
			map.put(EMAIL_INFORM, emailInfos.get(0));
		}

		return map;
	}

	@Remove
	public void remove() {

	}

}
